/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.sys.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.sys.dao.SettingsDao;
import com.chlitina.store.modules.sys.entity.Settings;

/**
 * 费率Service
 * @author lvbo
 * @version 2013-06-28
 */
@Component
@Transactional(readOnly = true)
public class SettingsService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(SettingsService.class);
	
	@Autowired
	private SettingsDao settingsDao;
	
	@Transactional(readOnly = false)
	public Settings getByKey(String key) {
		return settingsDao.findByKey(key);
	}
	
	public Page<Settings> find(Page<Settings> page, Settings settings) {
		List<Settings> all = settingsDao.findAll();
		page.setList(all);
		return page;
	}
	
	public Integer getIntValue(String key){
		Settings setValue = getByKey(key);
		if (setValue.getIntValue() != null) {
			return setValue.getIntValue();
		}
		return 0;
	}
	
	public Float getFloatValue(String key){
		Settings setValue = getByKey(key);
		if (setValue.getFloatValue() != null) {
			return setValue.getFloatValue();
		}
		return (float) 0;
	}
	
	public String getStringValue(String key){
		Settings setValue = getByKey(key);
		if (setValue.getStringValue() != null) {
			return setValue.getStringValue();
		}
		return "";
	}
	
	@Transactional(readOnly = false)
	public void save(Settings settings) {
		settingsDao.save(settings);
		settingsDao.refresh(settings);
	}
}
